<?php
namespace App\Http\Controllers\Wechat;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\OrderRepo;
use App\Repos\UserAddressRepo;
use App\Repos\UserCouponRepo;
use App\Repos\UserGroupbuyRepo;
use App\Repos\UserRepo;
use App\Http\Requests\Api\OrderRequest;


class OrderController extends Controller
{
    public function makeOrder(OrderRequest $request)
    {
    	$rs['status'] = false;
    	$data = $request->pure();
    	$data['code'] 		  = orderCode($request->type,$request->user_id);
    	
    	
    	$data['shop_id']      = (int)$request->shop_id;
    	$data['openid']       = $request->user->openid;

        // 如果使用了余额支付要看看余额是否正常
        if($request->user_money) {
            $usm['user_id'] = $request->user_id;
            $usm['shop_id'] = $request->shop_id;
            $userShopMoney  = UserRepo::getUserShopMoney($usm);
            //dd($userShopMoney);
            if($userShopMoney<$request->user_money) {
                $rs['msg'] = '余额数目不对'.$userShopMoney.':'.$request->user_money;
                return response()->json($rs);    
            }
        }
    	
    	if($request->address_id) {
    	   $userAddress      = UserAddressRepo::find(['id'=>$data['address_id']]);
           $data['address']  = $userAddress->full_address;    		
    	}

        if($request->user_coupon_id) {    
            $couponWhere['id']      = $request->user_coupon_id;
            $couponWhere['is_used'] = 0;      
            $uc = UserCouponRepo::find($couponWhere);
            if($uc){
                // 订单金额不能小于优惠券的满减金额
                if($request->total_money<object_get($uc,'coupon.min_amount')){
                    $rs['msg'] = '订单金额与优惠券不匹配';
                    return response()->json($rs);
                }
                $data['coupon'] = object_get($uc,'coupon.name');
            } else {
                $rs['msg'] = '优惠券不可用';
                return response()->json($rs);
            }
            
        }
    	
        
        $data['status'] = 1;    
        
    	
    	// 传过来 产品id和数量
    	$products = array_pull($data,'products');
    	//dd($products);
    	$ret = OrderRepo::create($data,$products);
    	if($ret) {
    		$rs['status'] = true;
    		$rs['data']   = $ret;
    	}
    	return response()->json($rs);
    }

    public function makeGroupOrder(OrderRequest $request)
    {
        $rs['status'] = false;
        $data = $request->pure();
        $data['code']         = orderCode($request->type,$request->user_id);
        
        
        $data['shop_id']      = (int)$request->shop_id;
        $data['openid']       = $request->user->openid;

        // 如果使用了余额支付要看看余额是否正常
        if($request->user_money) {
            $usm['user_id'] = $request->user_id;
            $usm['shop_id'] = $request->shop_id;
            $userShopMoney  = UserRepo::getUserShopMoney($usm);
            //dd($userShopMoney);
            if($userShopMoney<$request->user_money) {
                $rs['msg'] = '余额数目不对'.$userShopMoney.':'.$request->user_money;
                return response()->json($rs);    
            }
        }
        
        if($request->address_id) {
           $userAddress      = UserAddressRepo::find(['id'=>$data['address_id']]);
           $data['address']  = $userAddress->full_address;          
        }

        if($request->user_coupon_id) {    
            $couponWhere['id']      = $request->user_coupon_id;
            $couponWhere['is_used'] = 0;      
            $uc = UserCouponRepo::find($couponWhere);
            if($uc){
                // 订单金额不能小于优惠券的满减金额
                if($request->total_money<object_get($uc,'coupon.min_amount')){
                    $rs['msg'] = '订单金额与优惠券不匹配';
                    return response()->json($rs);
                }
                $data['coupon'] = object_get($uc,'coupon.name');
            } else {
                $rs['msg'] = '优惠券不可用';
                return response()->json($rs);
            }
            
        }
        
        $data['status'] = '0';
        // 传过来 产品id和数量
        $product = array_pull($data,'product');
       //dd($product);
        // 查看团购状态 user_groupbuy 表 product_id,user_id
        $ugbWhere['user_id']    = $request->user_id;
        $ugbWhere['product_id'] = $product['product_id'];
        $ugb = UserGroupbuyRepo::find($ugbWhere);
        if(object_get($ugb,'status')==100) { 
        //如果拼团成功 那么 订单状态为1 ,等待卖家确认
            $data['status']     = '1';
            
        }
        $data['group_code'] = object_get($ugb,'code');

        $ret = OrderRepo::createGoupOrder($data,$product);
        if($ret) {
            $rs['status'] = true;
            $rs['data']   = $ret;
        }
        return response()->json($rs);
    }

    

   
}
